# -*- coding: utf-8 -*-
"""
Created on Tue Dec 17 09:18:11 2019

@author: lenovo
"""

import pandas  as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns 
from  sklearn.cluster import DBSCAN
###############>>>>>>>>>>>>>>$$$$$$$$$$$$$$$$$$$<<<<<<<<<<<<<<<################

############>>>>>>>>>>>>>>Some critical parameters<<<<<<<<<<<<<<<##############

#计算指定区域的中心经度和纬度，并返回该区域内的轨迹数据
def interest_area_range(df,x_low,x_high,y_low,y_high):
    cond=(df['lon'] >= x_low) & (df['lon'] <= x_high)
    df=df[cond]
    cond=(df['lat'] >= y_low) & (df['lat'] <= y_high)
    df=df[cond]
    center_lon=(x_low+x_high)/2
    center_lat=(y_low+y_high)/2
    return df,center_lon,center_lat

#计算经度刻度数组    
#输入参数：查询区域经度上界、查询区域中心经度、网格经度方向尺寸    
def lon_scale_func(x_high,center_lon,size):
    lon_scale_list=[center_lon]
    lon_scale=center_lon
    i=1
    while lon_scale < x_high:
        lon_scale+=size#中心刻度高侧刻度
        less_lon_scale=lon_scale-2*i*size#中心刻度低侧刻度
        lon_scale_list.append(lon_scale)
        lon_scale_list.append(less_lon_scale)
        i+=1
    lon_scale_list.sort()
    grid_lon_scale=np.array(lon_scale_list)
    del lon_scale_list[0]
    lon_scale_array=np.array(lon_scale_list)
    return grid_lon_scale,lon_scale_array

#计算纬度刻度数组    
#输入参数：查询区域纬度上界、查询区域中心纬度、网格纬度方向尺寸 
#注意这里size可以与网格经度方向的size相同，也可以不同
def lat_scale_func(y_high,center_lat,size):
    lat_scale_list=[center_lat]
    lat_scale=center_lat
    i=1
    while lat_scale < y_high:
        lat_scale+=size#中心刻度高侧刻度
        less_lat_scale=lat_scale-2*i*size#中心刻度低侧刻度
        lat_scale_list.append(lat_scale)
        lat_scale_list.append(less_lat_scale)
        i+=1
    lat_scale_list.sort(reverse=True)
    grid_lat_scale=np.array(lat_scale_list)
    del lat_scale_list[0]
    lat_scale_array=np.array(lat_scale_list)
    return grid_lat_scale,lat_scale_array    

#为每个网格编号，即生成网格坐标与序号对应的字典
def index_for_grid(lon_scale_array,lat_scale_array):
    grid_num=int(len(lat_scale_array)*len(lon_scale_array))
    grid_index_list=list(range(grid_num))
    grid_tuple_list=[]
    for h in range(len(lat_scale_array)):
        for v in range(len(lon_scale_array)):
            grid_tuple_list.append((h,v))
    grid_dict=dict(zip(grid_tuple_list,grid_index_list))
    dict_grid=dict(zip(grid_index_list,grid_tuple_list))
    return grid_dict,dict_grid #生成网格字典，网格行列坐标对网格序号
                               #生成字典网格：网格序号对网格行列坐标


    
#判断两个点是否在同一网格内
def in_one_grid_or_not(pointA,pointB,lon_scale_array,lat_scale_array):
    lon_pointA=pointA[0]
    lat_pointA=pointA[1]
    lon_pointB=pointB[0]
    lat_pointB=pointB[1]
   
    cond1=lon_scale_array >= lon_pointA
    cond2=lon_scale_array >= lon_pointB
    grid_lon_A=lon_scale_array[cond1][0]
    grid_lon_B=lon_scale_array[cond2][0]
    cond3=lat_scale_array <= lat_pointA
    cond4=lat_scale_array <= lat_pointB
    grid_lat_A=lat_scale_array[cond3][0]
    grid_lat_B=lat_scale_array[cond4][0]
    if (grid_lon_A == grid_lon_B) and (grid_lat_A == grid_lat_B):
        return True
    else:
        return False

###############>>>>>>>>>>>>>>$$$$$$$$$$$$$$$$$$$<<<<<<<<<<<<<<<################    

###############>>>>>>>>>>>>>>Cross One Dim Or Not<<<<<<<<<<<<<<<###############
#判断两个点是否只穿越一个维度：
def cross_one_dim_or_not(pointA,pointB,lon_scale_array,lat_scale_array):
    lon_pointA=pointA[0]
    lat_pointA=pointA[1]
    lon_pointB=pointB[0]
    lat_pointB=pointB[1]
    
    cond1=lon_scale_array >= lon_pointA
    cond2=lon_scale_array >= lon_pointB
    grid_lon_A=lon_scale_array[cond1][0]
    grid_lon_B=lon_scale_array[cond2][0]
    cond3=lat_scale_array <= lat_pointA
    cond4=lat_scale_array <= lat_pointB
    grid_lat_A=lat_scale_array[cond3][0]
    grid_lat_B=lat_scale_array[cond4][0]
    #判断两个点是穿过lon还是穿过lat,
    if grid_lon_A == grid_lon_B:
        cross_one_dim=1#经度刻度一样，说明穿过lat
    elif grid_lat_A == grid_lat_B:
        cross_one_dim=2#纬度刻度一样说明穿过lon
    else:
        cross_one_dim=3#两个都不一样说明穿过网格数大于2
    return cross_one_dim,grid_lon_A,grid_lat_A
###############>>>>>>>>>>>>>>$$$$$$$$$$$$$$$$$$$<<<<<<<<<<<<<<<################

###########>>>>>>>>>>>>>>Lon Lat Between Two Points<<<<<<<<<<<<<<<#############
#cross_one_dim=1的情况,经度相同计算跨越纬度
def lat_between_two_points(pointA,pointB,lon_scale_array,lat_scale_array):
    lon_pointA=pointA[0]
    lat_pointA=pointA[1]
    lon_pointB=pointB[0]
    lat_pointB=pointB[1]
    #计算这两个点跨过的纬度刻度数组
    if lat_pointA <= lat_pointB:
        cond_lat=(lat_scale_array >= lat_pointA) & (lat_scale_array <= lat_pointB)
    else:
        cond_lat=(lat_scale_array >= lat_pointB) & (lat_scale_array <= lat_pointA)
    
    grid_lat_between=lat_scale_array[cond_lat]
    #计算这两个点的经度刻度
    return grid_lat_between

#cross_one_dim=2的情况，纬度相同计算跨越经度  
def lon_between_two_points(pointA,pointB,lon_scale_array,lat_scale_array):
    lon_pointA=pointA[0]
    lat_pointA=pointA[1]
    lon_pointB=pointB[0]
    lat_pointB=pointB[1]
    #计算这两个点跨过的纬度刻度数组
    if lon_pointA <= lon_pointB:
        cond_lon=(lon_scale_array >= lon_pointA) & (lon_scale_array <= lon_pointB)
    else:
        cond_lon=(lon_scale_array >= lon_pointB) & (lon_scale_array <= lon_pointA)
    
    grid_lon_between=lon_scale_array[cond_lon]
    #计算这两个点的纬度刻度  
    return grid_lon_between

#cross_one_dim=3的情况，计算跨越的经度和纬度      
def lon_lat_between_two_points(pointA,pointB,lon_scale_array,lat_scale_array):
    lon_pointA=pointA[0]
    lat_pointA=pointA[1]
    lon_pointB=pointB[0]
    lat_pointB=pointB[1]
    if (lon_pointA <= lon_pointB) and (lat_pointA <= lat_pointB):
        cond_lon=(lon_scale_array >= lon_pointA) & (lon_scale_array <= lon_pointB)
        cond_lat=(lat_scale_array >= lat_pointA) & (lat_scale_array <= lat_pointB)
    if (lon_pointA >= lon_pointB) and (lat_pointA <= lat_pointB):
        cond_lon=(lon_scale_array >= lon_pointB) & (lon_scale_array <= lon_pointA)
        cond_lat=(lat_scale_array >= lat_pointA) & (lat_scale_array <= lat_pointB)   
    if (lon_pointA >= lon_pointB) and (lat_pointA >= lat_pointB):
        cond_lon=(lon_scale_array >= lon_pointB) & (lon_scale_array <= lon_pointA)
        cond_lat=(lat_scale_array >= lat_pointB) & (lat_scale_array <= lat_pointA)   
    if (lon_pointB >= lon_pointA) and (lat_pointA >= lat_pointB):
        cond_lon=(lon_scale_array >= lon_pointA) & (lon_scale_array <= lon_pointB)
        cond_lat=(lat_scale_array >= lat_pointB) & (lat_scale_array <= lat_pointA)
    grid_lon_between=lon_scale_array[cond_lon]
    grid_lat_between=lat_scale_array[cond_lat]  
    return grid_lon_between,grid_lat_between    
###########>>>>>>>>>>>>>>$$$$$$$$$$$$$$$$$$$$$$$$$$<<<<<<<<<<<<<<<#############    

#################>>>>>>>>>>>>>>Pass Grid List<<<<<<<<<<<<<<<###################  
def linear_func(pointA,pointB):
    #这里轨迹点A和点B是轨迹中的点，且在顺序上点A在点B上
    x1=pointA[0]
    y1=pointA[1]
    x2=pointB[0]
    y2=pointB[1]
    
    #假设点A和点B构成的直线不是垂直的，
    k=(y2-y1)/(x2-x1)
    b=y1-x1*k
    return k,b


#计算只穿过lat的线段经过的网格坐标
def lat_pass_grid_list(grid_lon_A,grid_lat_between,lon_scale_array,lat_scale_array):
    pass_grid_list=[]
    cond=lon_scale_array == grid_lon_A
    verindex=np.argwhere(lon_scale_array == lon_scale_array[cond][0])[0][0]
    for j in range(len(grid_lat_between)):
        lat_value=grid_lat_between[j]
        #开始判断该lat交点在哪两个格子之间
        horindex=np.argwhere(lat_scale_array == lat_value)[0][0]
        grid1loc=(horindex,verindex)
        grid2loc=(horindex+1,verindex)
        pass_grid_list.append(grid1loc)
        pass_grid_list.append(grid2loc)
    pass_grid_list=list(set(pass_grid_list))
    return pass_grid_list

#计算只穿过lon的线段经过的网格坐标
def lon_pass_grid_list(grid_lat_A,grid_lon_between,lon_scale_array,lat_scale_array):
    pass_grid_list=[]
    cond=lat_scale_array == grid_lat_A
    horindex=np.argwhere(lat_scale_array == lat_scale_array[cond][0])[0][0]
    for j in range(len(grid_lon_between)):
        lon_value=grid_lon_between[j]
        #开始判断该lon交点在哪两个格子之间
        verindex=np.argwhere(lon_scale_array == lon_value)[0][0]
        grid1loc=(horindex,verindex)
        grid2loc=(horindex,verindex+1)
        pass_grid_list.append(grid1loc)
        pass_grid_list.append(grid2loc)
    pass_grid_list=list(set(pass_grid_list))
    return pass_grid_list

#计算又经过lon又经过lat的线段的网格坐标
def two_points_pass_grid_list(k,b,grid_lon_between,grid_lat_between,lon_scale_array,lat_scale_array):
    lon_cross_grid_list=[]
    for i in range(len(grid_lon_between)):
        lon_value=grid_lon_between[i]#交点的lon值
        lat_value=k*lon_value+b#交点的lat值
        #开始判断该lon交点在哪两个格子之间
        #寻找这个lon交点在哪条lon刻度上，该lon刻度索引是多少
        verindex=np.argwhere(lon_scale_array == lon_value)[0][0]
        #寻找该交点的lat刻度
        cond=lat_scale_array <= lat_value
        horindex=np.argwhere(lat_scale_array == lat_scale_array[cond][0])[0][0]
        grid1loc=(horindex,verindex)
        grid2loc=(horindex,verindex+1)
        lon_cross_grid_list.append(grid1loc)
        lon_cross_grid_list.append(grid2loc)
        
    lat_cross_grid_list=[]
    for j in range(len(grid_lat_between)):
        lat_value=grid_lat_between[j]
        lon_value=(lat_value-b)/k
        #开始判断该lat交点在哪两个格子之间
        #寻找这个lat交点在哪条lat刻度上，该lon刻度索引是多少
        horindex=np.argwhere(lat_scale_array == lat_value)[0][0]
        #寻找该交点的lat刻度
        cond=lon_scale_array >= lon_value
        verindex=np.argwhere(lon_scale_array == lon_scale_array[cond][0])[0][0]
        grid1loc=(horindex,verindex)
        grid2loc=(horindex+1,verindex)
        lat_cross_grid_list.append(grid1loc)
        lat_cross_grid_list.append(grid2loc) 
    
    pass_grid_list=list(set(lon_cross_grid_list) | set(lat_cross_grid_list))
    return pass_grid_list
###########>>>>>>>>>>>>>>$$$$$$$$$$$$$$$$$$$$$$$$$$<<<<<<<<<<<<<<<#############

#穿越网格的起始点所在网格坐标
def pointA_grid(pointA,lon_scale_array,lat_scale_array):
    lonA=pointA[0]
    latA=pointA[1]
    cond_lon=lon_scale_array >= lonA
    lon_value=lon_scale_array[cond_lon][0]
    verindex=np.argwhere(lon_scale_array == lon_value)[0][0]
    cond_lat=lat_scale_array <= latA
    lat_value=lat_scale_array[cond_lat][0]
    horindex=np.argwhere(lat_scale_array == lat_value)[0][0]
    pointA_loc=(horindex,verindex)
    return pointA_loc

############################################################################
##########................轨迹的网格穿越数目主函数................############
############################################################################

#对子轨迹列表内的每段轨迹计算穿越网格热度
#输出数组形式的网格热度以及
#一个列表，该列表内包含许多小列表，每个小列表内包含一条子轨迹所经过的网格序号(该列表后来没有被使用，可以不看)   
def cross_num_heat_array(subtraj_list,lon_scale_array,lat_scale_array,grid_dict):
    #df可以为指定区域轨迹数据也可以是一艘船的全部轨迹数据
    #SSL代表某段子轨迹的时间戳列表
    #SSL_list就是储存了所有子轨迹时间戳列表的列表
    heat_array=np.zeros((len(lat_scale_array),len(lon_scale_array))) 
    grid_sequence_all_subtraj=[]
    for tra_df in subtraj_list:
        pass_grid_of_subtraj=''
        for i in range(len(tra_df)-1):
            pointA=tra_df[['lon','lat']].values[i]
            pointB=tra_df[['lon','lat']].values[i+1]
            #判断相邻的两个点组成的线段是否在一个网格内
            if in_one_grid_or_not(pointA,pointB,lon_scale_array,lat_scale_array):
                continue
            else:
                #判断这个线段是只穿过纬度、只穿过经度、还是都穿过
                cross_one_dim,grid_lon_A,grid_lat_A=cross_one_dim_or_not(pointA,pointB,lon_scale_array,lat_scale_array)
                #通过穿过态势判断该线段的纬度刻度数组还是经度刻度数组
                if cross_one_dim == 1:
                    grid_lat_between=lat_between_two_points(pointA,pointB,lon_scale_array,lat_scale_array)
                elif cross_one_dim == 2:
                    grid_lon_between=lon_between_two_points(pointA,pointB,lon_scale_array,lat_scale_array)
                else:
                    grid_lon_between,grid_lat_between=lon_lat_between_two_points(pointA,pointB,lon_scale_array,lat_scale_array)
                #通过穿过态势和刻度数组
                if cross_one_dim == 1:
                    pass_grid_list=lat_pass_grid_list(grid_lon_A,grid_lat_between,lon_scale_array,lat_scale_array)
                elif cross_one_dim == 2:
                    pass_grid_list=lon_pass_grid_list(grid_lat_A,grid_lon_between,lon_scale_array,lat_scale_array)
                else:
                    k,b=linear_func(pointA,pointB)
                    pass_grid_list=two_points_pass_grid_list(k,b,grid_lon_between,grid_lat_between,lon_scale_array,lat_scale_array)   
                    
            #将网格坐标列表中的起始点所在的网格坐标去掉
            pointA_tuple=pointA_grid(pointA,lon_scale_array,lat_scale_array)
            #但是这种情况在经过顶点时会出错，对于经过顶点的情况直接跳过
            if pointA_tuple in pass_grid_list:
                pass_grid_list.remove(pointA_tuple)
            for j in range(len(pass_grid_list)):
                loc_tuple=pass_grid_list[j]
                heat_array[loc_tuple[0],loc_tuple[1]]+=1
            for grid_loc_tuple in pass_grid_list:
                pass_grid_of_subtraj+=(str(grid_dict[grid_loc_tuple])+',')
        
        pointA=tra_df[['lon','lat']].values[0]
        pointA_tuple=pointA_grid(pointA,lon_scale_array,lat_scale_array)
        if pass_grid_of_subtraj != '' :
            pass_grid_of_subtraj=str(grid_dict[pointA_tuple])+','+pass_grid_of_subtraj
            pass_grid_of_subtraj=pass_grid_of_subtraj[:-1]
        else:
            pass_grid_of_subtraj='A'+str(grid_dict[pointA_tuple])+','+pass_grid_of_subtraj
            pass_grid_of_subtraj=pass_grid_of_subtraj[:-1]
        grid_sequence_all_subtraj.append(pass_grid_of_subtraj)
        
    return heat_array,grid_sequence_all_subtraj


#利用核密度估计绘制该区域网格穿越数目的空间分布
def grid_frequency(heat_array):
    #生成n*2维的网格坐标数组，n是网格的总数，2是因为每个网格坐标是二维的
    grid_pos_list=[]
    for i in range(heat_array.shape[0]):
        for j in range(heat_array.shape[1]):
            grid_pos_list.append([i,j])
    grid_pos_array=np.reshape(grid_pos_list,(heat_array.shape[0]*heat_array.shape[1],2))
    #生成一个网格对应热度的列表
    grid_heat_list=[]
    for m in range(heat_array.shape[0]):
        for n in range(heat_array.shape[1]):
            grid_heat_list.append(heat_array[m,n])
    #复制grid_pos_array中的网格坐标，使得坐标出现的次数对应其热度
    grid_frequency_array=grid_pos_array.copy()
    grid_frequency_array=grid_frequency_array.repeat(grid_heat_list,axis=0)
    df = pd.DataFrame(grid_frequency_array, columns=["lat", "lon"])
    df['lat']=heat_array.shape[0]-1-df['lat']
    sns.jointplot(x="lon", y="lat", data=df, kind="kde")
    plt.show()
    #返回的就是包含每个网格坐标的n*2维数组        
    return grid_pos_array,grid_frequency_array
            

############################################################################
#############................区域重心坐标计算................################
############################################################################    

#计算网格经纬度范围数组
def grid_lon_lat_func(grid_lon_scale,grid_lat_scale):
    grid_loclist=[]
    for i in range(len(grid_lat_scale)-1):
        grid_loclist_yaxis=[]
        y_value_high=grid_lat_scale[i]
        y_value_low=grid_lat_scale[i+1]
        for j in range(len(grid_lon_scale)-1):
            x_value_low=grid_lon_scale[j]
            x_value_high=grid_lon_scale[j+1]
            grid_array=[x_value_low,x_value_high,y_value_low,y_value_high]
            grid_loclist_yaxis.append(grid_array)
        grid_loclist.append(grid_loclist_yaxis)
    grid_locarray=np.reshape(grid_loclist,(len(grid_lat_scale)-1,len(grid_lon_scale)-1,4))
    return grid_locarray  #三维的网格四角坐标数组


#计算网格中心坐标数组
def grid_center_loc(lon_scale_array,lat_scale_array,grid_locarray):
    grid_center_loc_array=np.zeros((len(lat_scale_array),len(lon_scale_array),2))
    grid_tuple=[]
    grid_center_tuple=[]
    for i in range(len(lat_scale_array)):
        for j in range(len(lon_scale_array)):
            low_lon=grid_locarray[i,j][0]
            high_lon=grid_locarray[i,j][1]
            low_lat=grid_locarray[i,j][2]
            high_lat=grid_locarray[i,j][3]
            center_lon=(low_lon+high_lon)/2
            center_lat=(low_lat+high_lat)/2
            grid_center_loc_array[i,j,0]=center_lon
            grid_center_loc_array[i,j,1]=center_lat
            grid_tuple.append((i,j))
            grid_center_tuple.append((center_lon,center_lat))
    
    center_tuple_dict=dict(zip(grid_center_tuple,grid_tuple))
    return grid_center_loc_array,center_tuple_dict   #生成三维的网格中心坐标数组
                                                     #生成网格中心坐标元祖对网格行列元组的字典
'''
#生成每个子轨迹中所有点对应的网格序号列表
def grid_index_for_subpoints(subtraj_df_list,lon_scale_array,lat_scale_array,grid_dict):
    alist=[]
    for subtraj in subtraj_df_list:
        blist=[]
        for i in range(len(subtraj)):    
            pointA=subtraj[['lon','lat']].values[i]
            pointA_tuple=pointA_grid(pointA,lon_scale_array,lat_scale_array)
            blist.append(grid_dict[pointA_tuple])
        alist.append(blist)
    return alist #生成一个列表，列表中有很多小列表，小列表中含有一条子轨迹所有点所在的网格的行列元组
'''

def grid_index_for_subpoints(subtraj_df_list,lon_scale_array,lat_scale_array):
    alist=[]
    for subtraj in subtraj_df_list:
        blist=[]
        for i in range(len(subtraj)):    
            pointA=subtraj[['lon','lat']].values[i]
            pointA_tuple=pointA_grid(pointA,lon_scale_array,lat_scale_array)
            blist.append(pointA_tuple)
        alist.append(blist)
    return alist #生成一个列表，列表中有很多小列表，小列表中含有一条子轨迹所有点所在的网格的行列元组


#生成由几个网格构成的区域所包含的子轨迹序号列表
#SPGIL:subtraj_point_grid_index_list
def subtraj_index_in_area(grid_index_list,alist,ratio_threshold):
    subtraj_index_list=[]
    for i in range(len(alist)):
        SPGIL=alist[i]
        cover_point_num=0
        for grid_index in SPGIL:
            if grid_index in grid_index_list:
                cover_point_num+=1
        cover_rate=cover_point_num/len(SPGIL)
        if cover_rate >= ratio_threshold:
            subtraj_index_list.append(i)
    return subtraj_index_list
    
#根据一个子轨迹集合subtraj_list及网格中心坐标数组和半径参数，生成每个网格的平均速度  
def speed_grid_func(subtraj_list,grid_center_loc_array,radium):
    all_point=pd.concat(subtraj_list)
    a=grid_center_loc_array.shape[0]
    b=grid_center_loc_array.shape[1]
    speed_heat_array=np.zeros((a,b))
    speed_list=[]
    for i in range(grid_center_loc_array.shape[0]):
        for j in range(grid_center_loc_array.shape[1]):
            grid_center_loc=grid_center_loc_array[i,j]
            low_lon=grid_center_loc[0]-radium
            high_lon=grid_center_loc[0]+radium
            low_lat=grid_center_loc[1]-radium
            high_lat=grid_center_loc[1]+radium
            cond1=(all_point['lon'] >= low_lon) & (all_point['lon'] <= high_lon)
            cond2=(all_point['lat'] >= low_lat) & (all_point['lat'] <= high_lat)
            neighbour_point=all_point[cond1 & cond2]
            if len(neighbour_point) == 0:
                speed_heat_array[i,j]=-1
                speed_list.append(-1)
            else:
                c=0
                d=0
                for k in range(len(neighbour_point)):
                    point_loc=neighbour_point[['lon','lat']].iloc[k].values
                    point_dist=np.linalg.norm(point_loc-grid_center_loc)
                    point_speed=neighbour_point['sog'].iloc[k]
                    c+=(point_dist**(-2))*point_speed
                    d+=point_dist**(-2)
                grid_speed=c/d*3.6/1.852
                speed_heat_array[i,j]=grid_speed
                speed_list.append(grid_speed)
    index_speed_dict=dict(zip(range(grid_center_loc_array.shape[0]*grid_center_loc_array.shape[1]),speed_list))
            
    return speed_heat_array,index_speed_dict

            
#根据一个网格中心坐标数组进行聚类      
#pgcla：partion_grid_center_location_array，部分网格中心坐标数组         
def grid_cluster(pgcla,eps,min_samples,center_tuple_dict):
    dbscan = DBSCAN(eps, min_samples)
    dbscan.fit(pgcla) 
    label_pred = dbscan.labels_
    area_class_array=np.unique(label_pred)
    print(area_class_array)
    
    all_area_grid_tuple_list=[]
    colorstyle=['orange','blue','red','black','green','yellow']*10
    for i in range(len(area_class_array)):
        area_class=area_class_array[i]
        area_loc_center_array=pgcla[label_pred==area_class]  #########
        plt.scatter(area_loc_center_array[:, 0], area_loc_center_array[:, 1],c=colorstyle[i])
        area_grid_tuple_list=[]
        for j in range(len(area_loc_center_array)):
            area_loc_center_tuple=(area_loc_center_array[j,0],area_loc_center_array[j,1])
            grid_tuple=center_tuple_dict[area_loc_center_tuple]#找到该网格中心坐标元祖对应的行列元祖
            area_grid_tuple_list.append(grid_tuple)
        all_area_grid_tuple_list.append(area_grid_tuple_list)
    plt.show()       
    return label_pred,all_area_grid_tuple_list

    
           
  









